<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="small"
          @click="handleSearch"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['output:proShippedStart:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['output:proShippedStart:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['output:proShippedStart:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="small"
          :disabled="single"
          @click="handleSubmit"
          v-hasPermi="['output:proShippedStart:submit']"
        >
          提交
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExport(1)"
          v-hasPermi="['output:proShippedStart:export']"
        >
          EXCEL
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExport(2)"
          v-hasPermi="['output:proShippedStart:export']"
        >
          出货明细
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExport(3)"
          v-hasPermi="['output:proShippedStart:export']"
        >
          出货统计
        </el-button>
      </el-col>

      <Tag type="dot" size="default" color="#0fac09">提交</Tag>
      <Tag type="dot" size="default" color="#bb0331">通过</Tag>
      <Tag type="dot" size="default" color="#0323af">检验</Tag>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :cell-style="iCellStyle"
      :data="chmMstrList"
      :height="tableH"
      @row-click="clickChm"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" width="40" type="index" align="center" />
      <el-table-column
        label="出货单号"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="chmDjbh"
      />
      <el-table-column
        label="出货日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="chmChrq"
        width="150"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.chmChrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="出货类型"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmType"
      >
        <template #default="scope">
          <dict-tag :options="allOutTypeOptions" :value="scope.row.chmType" />
        </template>
      </el-table-column>
      <el-table-column
        label="客户"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmKhdm"
      >
        <template #default="scope">
          <dict-tag :options="allCustomerOptions" :value="scope.row.chmKhdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="出货仓库"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.chmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="开单人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmJlry"
      />
      <el-table-column
        label="开单日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="chmJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.chmJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="chmShrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.chmShrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="chmGzrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.chmGzrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmNgdesc"
      />
      <el-table-column
        label="操作"
        align="center"
        width="100"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="small"
            type="text"
            @click="handleAddChildren(scope.row)"
            v-hasPermi="['output:proShippedStart:edit']"
            :disabled="scope.row.chmTjflag != 0"
          >
            添加物料
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    添加对话框-->
    <el-dialog
      v-model="openFull"
      :fullscreen="true"
      :close-on-click-modal="false"
      append-to-body
    >
      <!-- 子项搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-row>
          <el-col :span="6">
            <el-form-item label="物料代码" prop="chdWldm">
              <el-input
                v-model="queryChildParams.chdWldm"
                placeholder="请输入物料代码"
                clearable
                size="small"
                @keyup.enter.native="serachChildrened"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规则描述" prop="itmPmgg">
              <el-input
                v-model="queryChildParams.itmPmgg"
                placeholder="请输入规则描述"
                clearable
                size="small"
                @keyup.enter.native="serachChildrened"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              style="
                display: flex;
                justify-content: flex-end;
                margin-right: 150px;
              "
            >
              <el-button type="primary" @click="serachChildrened"
                >查询</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!--子项数据框-->
      <!--      @row-click="clickdel"-->
      <el-form>
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="serachAddList"
          border
          stripe
          :height="tableH"
          @scrollBottom="load2"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            min-width="5%"
            type="index"
            align="center"
          />
          <el-table-column
            label="客户代码"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmCustwldm"
          />
          <el-table-column
            label="销售单号"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="chdSodh"
          />
          <el-table-column
            label="物料代码"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="chdWldm"
          />
          <el-table-column
            label="品名规格"
            min-width="30%"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="交货数量"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="jhmPqsl"
          />
          <el-table-column
            label="已交货数"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="jhmYjhs"
          />
          <el-table-column
            label="超发数量"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="jhmCfsl"
          />
          <el-table-column
            label="待交货数"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="jhmDjhs"
          />
          <el-table-column
            label="可用库存"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="kcmKcsl"
          />
          <el-table-column
            label="需申请数"
            min-width="10%"
            align="center"
            prop="chdCksl"
          >
            <template #default="scope">
              <el-input
                class="inputName"
                @input="changValue(scope.row)"
                v-model="scope.row.chdCksl"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            label="每箱数"
            min-width="10%"
            align="center"
            prop="chdMinbz"
          >
            <template #default="scope">
              <el-input
                class="inputName"
                placeholder="非必填"
                v-model="scope.row.chdMinbz"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="总箱数"
            min-width="10%"
            align="center"
            prop="chdBzsl"
          >
            <template #default="scope">
              <el-input
                class="inputName"
                placeholder="非必填"
                v-model="scope.row.chdBzsl"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="生产日期"
            min-width="10%"
            align="center"
            prop="chdScrq"
          >
            <template #default="scope">
              <el-date-picker
                clearable
                size="small"
                v-model="scope.row.chdScrq"
                type="date"
                :clearable="false"
                value-format="yyyy-MM-dd"
              />
            </template>
          </el-table-column>
        </el-table>
        <el-tabs v-model="activeTab">
          <el-tab-pane label="出货明细" name="first">
            <el-table
              border
              stripe
              :height="tableHAddChild"
              :data="chdDetList"
              :header-cell-style="{ 'text-align': 'center' }"
            >
              <el-table-column
                label="#"
                type="index"
                align="center"
                fixed="left"
              />
              <el-table-column
                label="客户编号"
                align="center"
                prop="itmCustwldm"
                fixed="left"
              />
              <el-table-column label="物料代码" align="center" prop="chdWldm" />
              <el-table-column label="规格描述" align="center" prop="itmPmgg" />
              <el-table-column
                label="单位"
                :show-overflow-tooltip="true"
                align="center"
                prop="itmDw"
              >
                <template #default="scope">
                  <dict-tag
                    :options="unitFlagOptions"
                    :value="scope.row.itmDw"
                  />
                </template>
              </el-table-column>
              <el-table-column
                label="存放位置"
                :show-overflow-tooltip="true"
                align="center"
                prop="chdCwdm"
              >
                <template #default="scope">
                  <dict-tag
                    :options="allCwOptions"
                    :value="scope.row.chdCwdm"
                  />
                </template>
              </el-table-column>
              <el-table-column label="出库数量" align="center" prop="chdCksl" />
              <el-table-column label="出库人员" align="center" prop="chdCkry" />
              <el-table-column label="出库日期" align="center" prop="chdCkrq">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.rtdRkrq, "{y}-{m}-{d}") }}</span>
                </template>
              </el-table-column>
              <el-table-column label="销售单号" align="center" prop="chdSodh" />
              <el-table-column
                fixed="right"
                label="操作"
                align="center"
                width="100"
                class-name="small-padding fixed-width"
              >
                <template #default="scope">
                  <el-button
                    size="mini"
                    type="text"
                    :disabled="single"
                    v-hasPermi="['output:proShippedStart:deleteChild']"
                    @click="deleteChdDet(scope.row)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="出货统计" name="second">
            <el-table
              border
              stripe
              :height="tableHAddChild"
              :data="chdDetTotalList"
            >
              <el-table-column
                label="#"
                type="index"
                align="center"
                fixed="left"
              />
              <el-table-column
                label="客户编号"
                align="center"
                prop="itmCustwldm"
                fixed="left"
              />
              <el-table-column label="物料代码" align="center" prop="chdWldm" />
              <el-table-column label="规格描述" align="center" prop="itmPmgg" />
              <el-table-column
                label="单位"
                :show-overflow-tooltip="true"
                align="center"
                prop="itmDw"
              >
                <template #default="scope">
                  <dict-tag
                    :options="unitFlagOptions"
                    :value="scope.row.itmDw"
                  />
                </template>
              </el-table-column>
              <el-table-column label="出库数量" align="center" prop="chdCksl" />
            </el-table>
          </el-tab-pane>
        </el-tabs>
        <el-form-item
          style="margin-top: 25px; display: flex; justify-content: flex-end"
        >
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="handAddChildren">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="出货明细" name="first">
        <el-table
          border
          stripe
          :height="tableHChild"
          :data="chdDetList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column
            label="客户编号"
            align="center"
            prop="itmCustwldm"
            fixed="left"
          />
          <el-table-column label="超发单号" align="center" prop="chdCfdh" />
          <el-table-column label="物料代码" align="center" prop="chdWldm" />
          <el-table-column label="规格描述" align="center" prop="itmPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="存放位置"
            :show-overflow-tooltip="true"
            align="center"
            prop="chdCwdm"
          >
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.chdCwdm" />
            </template>
          </el-table-column>
          <el-table-column label="每箱数" align="center" prop="chdMinbz" />
          <el-table-column label="总箱数" align="center" prop="chdBzsl" />
          <el-table-column label="出库数量" align="center" prop="chdCksl" />
          <el-table-column label="出库人员" align="center" prop="chdCkry" />
          <el-table-column label="出库日期" align="center" prop="chdCkrq" />
          <el-table-column label="销售单号" align="center" prop="chdSodh" />
          <el-table-column
            label="箱号"
            width="120"
            align="center"
            prop="rrqXs"
          />
          <el-table-column
            label="生产日期"
            align="center"
            prop="chdScrq"
            width="150"
          ></el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                :disabled="single"
                @click="deleteChdDet(scope.row)"
                v-hasPermi="['output:cjmOpen:deleteCjd']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="出货统计" name="second">
        <el-table border stripe :height="tableHChild" :data="chdDetTotalList">
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column
            label="客户编号"
            align="center"
            prop="itmCustwldm"
            fixed="left"
          />
          <el-table-column label="物料代码" align="center" prop="chdWldm" />
          <el-table-column label="规格描述" align="center" prop="itmPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column label="出库数量" align="center" prop="chdCksl" />
          <el-table-column label="每箱数" align="center" prop="chdMinbz" />
          <el-table-column label="总箱数" align="center" prop="chdBzsl" />
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="开单日期">
            <el-date-picker
              v-model="daterangeChmJlrq"
              size="small"
              :picker-options="pickerOptions"
              style="width: 280px"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="出货单号" prop="chmDjbh">
            <el-input
              v-model="queryParams.chmDjbh"
              style="width: 280px"
              placeholder="请输入出货单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="出货仓库" prop="chmCkdm">
            <el-select
              v-model="queryParams.chmCkdm"
              placeholder="请选择出货仓库"
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <RadioGroup
            v-model="queryParams.chmZtbz"
            style="width: 280px; margin-left: 68px"
          >
            <Radio label="1" style="margin: 0px 30px">未审核</Radio>
            <Radio label="2" style="margin-right: 30px">已审核</Radio>
            <Radio label="0" align="right">全部</Radio>
          </RadioGroup>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改产品出货对话框 -->
    <el-dialog
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="680px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="88px">
        <el-col :span="12">
          <el-form-item label="出货单号" prop="chmDjbh">
            <el-input
              v-model="form.chmDjbh"
              placeholder="流水单号生成"
              style="width: 217px"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出货日期" prop="chmChrq">
            <el-date-picker
              :disabled="notEdit"
              clearable
              size="small"
              v-model="form.chmChrq"
              type="date"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择出货日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出货类型" prop="chmType">
            <el-select
              v-model="form.chmType"
              :disabled="notEdit"
              placeholder="请选择出货类型"
              @change="selectionBoxChange"
              clearable
            >
              <el-option
                v-for="item in outTypeOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出货仓库" prop="chmCkdm">
            <el-select
              v-model="form.chmCkdm"
              :disabled="notEdit"
              placeholder="请选择出货仓库"
              clearable
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="客户代码" prop="chmKhdm">
            <el-select
              v-model="form.chmKhdm"
              :disabled="notEdit"
              placeholder="请选择客户代码"
              clearable
            >
              <el-option
                v-for="item in customerOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="单据备注" prop="chmDesc">
            <el-input
              type="textarea"
              v-model="form.chmDesc"
              placeholder="请输入单据备注"
            />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listChmMstr,
  listChdDet,
  getProShipped,
  delChmMstr,
  delChdDet,
  addProShipped,
  updateProShipped,
  exportProShipped,
  getNeedData,
  submitChm,
  sumitChild,
} from "@/api/output/proShipped";
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { getAllCustomer, getAllCustomerTwo } from "@/api/project/customer";

export default {
  name: "proShippedStart",
  data() {
    return {
      isNew: true,
      // 遮罩层
      loading: true,
      openFull: false,
      // 导出遮罩层
      exportLoading: false,
      //表格高度
      tableH: 250,
      tableHChild: 300,
      tableHAdd: 250,
      tableHAddChild: 300,
      activeTab: "first",
      // 有效和所有的出货类型字典
      allOutTypeOptions: [],
      outTypeOptions: [],
      //单位字典
      unitFlagOptions: [],
      //默认储位字典
      allCwOptions: [],
      //有效仓库和所有仓库
      storageOptions: [],
      allStorageOptions: [],
      serachAddList: [],

      //客户类型和所有客户类型
      customerOptions: [],
      allCustomerOptions: [],
      // 选中id
      ids: null,
      //选中的订单编号
      chmDjbhs: null,
      chmMstrs: {},
      //子项选中的订单编号
      chmDjbhc: null,
      chmTypec: null,
      // 非单个禁用
      single: true,

      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      total2: 0,
      // 产品出货表格数据
      chmMstrList: [],
      // 产品出货详细表格数据
      chdDetList: [],
      // 产品出货统计表格数据
      chdDetTotalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 添加物料显示弹出层
      addChildOpen: false,
      // 开单日期时间范围
      daterangeChmJlrq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      rowdata: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        chmDjbh: null,
        chmZtbz: "1",
        chmJlrq: null,
        chmCkdm: "",
        index: 0,
      },
      // 添加物料-查询参数
      queryChildParams: {
        pageNum: 1,
        pageSize: 20,
        chdDjbh: null,
        itmPmgg: null,
        chdWldm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        chmChrq: [
          { required: true, message: "出货日期不能为空", trigger: "blur" },
        ],
        chmType: [
          { required: true, message: "出货类型不能为空", trigger: "blur" },
        ],
        chmCkdm: [
          { required: true, message: "出货仓库不能为空", trigger: "blur" },
        ],
        chmKhdm: [
          { required: true, message: "客户代码不能为空", trigger: "blur" },
        ],
      },
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    //所有出货类型
    this.getAllDicts("sys_out_type").then((response) => {
      this.allOutTypeOptions = response.data;
    });
    //有效出货类型
    this.getDicts("sys_out_type").then((response) => {
      this.outTypeOptions = response.data;
    });
    //有效仓库
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;
      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //所有仓库
    getAll().then((response) => {
      this.allStorageOptions = response.data;
      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    // 有效客户
    getAllCustomer().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode + "";
        i.listClass = "default";
      }
      this.customerOptions = response.data;
    });
    //所有客户
    getAllCustomerTwo().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode + "";
        i.listClass = "default";
      }
      this.allCustomerOptions = response.data;
    });
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //存放位置
    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });
  },
  computed: {
    notEdit() {
      return (
        !this.isNew && this.chdDetTotalList && this.chdDetTotalList.length > 0
      );
    },
  },
  methods: {
    /** 颜色事件 */
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.chmZtbz == "1" && row.chmTjflag == "1") {
        return "color:#0fac09";
      }
      //通过
      else if (row.chmZtbz == "2" && row.chmTjflag == "1") {
        return "color:#0323af";
      }
      //检验
      else if (row.chmZtbz == "6" && row.chmTjflag == "1") {
        return "color:#bb0331";
      }
    },
    /** 查询产品出货列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeChmJlrq && "" != this.daterangeChmJlrq) {
        this.queryParams.params["beginChmJlrq"] = this.moment(
          this.daterangeChmJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endChmJlrq"] = this.moment(
          this.daterangeChmJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }

      listChmMstr(this.queryParams).then((response) => {
        this.chmMstrList = [];
        this.chmMstrList = this.chmMstrList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickChm(row);
        } else if (this.chmMstrList != null && this.chmMstrList.length > 0) {
          this.clickChm(this.chmMstrList[0]);
        }
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 20;
        this.queryParams.pageNum++;
        listChmMstr(this.queryParams).then((response) => {
          this.chmMstrList = this.chmMstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    changValue(row) {
      if (row.chdCksl > row.kcmKcsl) {
        this.msgError("不允许超过可用库存");
        row.chdCksl = row.kcmKcsl;
      }
      // if ('CK03'!= this.chmTypec && row.chdCksl > row.jhmDjhs) {
      //   this.msgError('不允许超过待交货数')
      //   row.chdCksl = row.jhmDjhs
      // }
    },
    getChdDetList(chdDjbh) {
      listChdDet(chdDjbh).then((response) => {
        this.chdDetTotalList = response.data.total;
        this.chdDetList = response.data.detail;
      });
    },

    handAddChildren() {
      const datas = this.serachAddList.filter((a) => {
        return !!a.chdCksl && a.chdCksl > 0;
      });
      if (datas.length < 1) {
        this.msgError("请输入合法数量~");
        return;
      }
      sumitChild(datas)
        .then((response) => {
          this.msgSuccess("添加成功~");
          this.queryChildParams.pageNum = 1;
          this.serachAddList = [];
          this.serachChildrened();
          this.getChdDetList(this.chmDjbhc);
        })
        .catch(() => {
          this.msgError("添加失败~");
        });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.openFull = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chmId: null,
        chmDjbh: null,
        chmType: null,
        chmChrq: new Date(),
        chmCkdm: null,
        chmKhdm: null,
        chmDesc: null,
        chmTjflag: null,
        chmZtbz: null,
        chmNgdesc: null,
        chmJlry: null,
        chmJlrq: null,
        chmShry: null,
        chmShrq: null,
        chmGzry: null,
        chmGzrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.chmMstrList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeChmJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //单击选中数据
    clickChm(row) {
      if (row != null) {
        this.single = !(
          row != null &&
          row.chmZtbz == "1" &&
          row.chmTjflag == "0"
        );
        this.ids = row.chmId;
        this.chmMstrs = row;
        this.chmDjbhs = row.chmDjbh;
        this.getChdDetList(row.chmDjbh);
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isNew = true;
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.isNew = false;
      this.reset();
      const chmId = this.ids;
      getProShipped(chmId).then((response) => {
        this.form = response.data;
        this.chdDetList = response.data.chdDetList;
        this.open = true;
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询产品出货";
      this.openSearch = true;
    },
    /** 新增和更新-提交操作 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.chmId != null) {
            updateProShipped(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.queryParams.pageNum = 1;
              this.chmMstrList = [];
              this.getList();
            });
          } else {
            this.form.chmChrq = this.moment(this.form.chmChrq).format(
              "YYYY-MM-DD HH:mm:ss"
            );
            addProShipped(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.queryParams.pageNum = 1;
              this.chmMstrList = [];
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete() {
      const chmDjbh = this.chmDjbhs;
      this.$confirm('是否确认删除出货单号"' + chmDjbh + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return delChmMstr(chmDjbh);
        })
        .then(() => {
          this.queryParams.pageNum = 1;
          this.total = 0;
          this.chmMstrList = [];
          listChmMstr(this.queryParams).then((response) => {
            // this.chmMstrList = []
            this.chmMstrList = this.chmMstrList.concat(response.rows);
            this.total = response.total;
            this.loading = false;
          });
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 删除子项明细操作 */
    deleteChdDet(row) {
      delChdDet(row.chdId)
        .then((res) => {
          this.msgSuccess("删除成功~");
          this.getChdDetList(row.chdDjbh);
        })
        .catch(() => {
          this.msgError("删除失败~");
        });
    },
    /** 提交操作按钮 */
    handleSubmit() {
      if (this.chmDjbhs == null) {
        this.msgError("请选择需要提交的单据");
      } else {
        listChdDet(this.chmDjbhs).then((res) => {
          if (res.data.detail.length <= 0) {
            this.msgError("该单据下没有物料,无法提交");
          } else {
            submitChm(this.chmDjbhs)
              .then((res) => {
                this.msgSuccess("提交成功~");
                this.queryParams.pageNum = 1;
                this.chmMstrList = [];
                this.getList();
              })
              .catch(() => {
                this.msgError("提交失败~");
              });
          }
        });
      }
    },
    serachChildrened() {
      this.queryChildParams.pageNum = 1;
      getNeedData(this.queryChildParams).then((res) => {
        this.serachAddList = [];
        this.serachAddList = this.serachAddList.concat(res.rows);
        this.total2 = res.total;
      });
    },
    /** 添加物料 */
    handleAddChildren(row) {
      // this.addChildOpen = true;
      this.openFull = true;
      this.chmDjbhc = row.chmDjbh;
      this.chmTypec = row.chmType;
      this.getChildNeedData();
      // this.serachChildrened()
    },
    /** 查询需要添加的物料*/
    getChildNeedData() {
      this.queryChildParams.chdDjbh = this.chmDjbhc;
      getNeedData(this.queryChildParams).then((res) => {
        this.serachAddList = [];
        this.serachAddList = this.serachAddList.concat(res.rows);
      });
    },
    load2() {
      if (
        this.queryChildParams.pageNum <
        Math.ceil(this.total2 / this.queryChildParams.pageSize)
      ) {
        this.queryChildParams.pageSize = 20;
        this.queryChildParams.pageNum++;
        getNeedData(this.queryChildParams).then((response) => {
          this.serachAddList = this.serachAddList.concat(response.rows);
          this.total2 = response.total;
        });
      }
      return 0;
    },
    /** 导出按钮操作 */
    handleExport(index) {
      this.queryParams.chmDjbh = this.chmDjbhs;
      this.queryParams.index = index;
      if (index == 2 || index == 3) {
        if (
          this.queryParams.chmDjbh == "" ||
          this.queryParams.chmDjbh == null
        ) {
          this.$Modal.warning({
            title: "提示",
            content: "请选择需要导出的单据",
          });
        }
      }
      const queryParams = this.queryParams;
      exportProShipped(queryParams)
        .then((res) => {
          this.download(res.msg);
        })
        .catch(() => {});
    },
    /** 若出货类型为第三方出货提醒事件 */
    selectionBoxChange() {
      if (this.form.chmType === "CK02") {
        this.$confirm("确认选择第三方出货？", "确认", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning",
        })
          .then(() => {})
          .catch(() => {
            this.form.chmType = null;
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
